// pages/my/my-address/my-address.js
const request = require('../../../../../utils/request')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //默认
        circle: false,
        status: 0,
        list: [],
        isSelAddress: false, //是否是确认订单选址
    },
    onLoad(options) {
        this.setData({
            isSelAddress: options.type == 1 ? true : false
        })

    },
    onShow() {
        this.getList()
    },
    /**
     * 控制显示按钮
     */
    show() {
        this.setData({
            circle: !this.data.circle
        })
    },

    getList() {
        let _this = this
        request._post('/api?method=user.getusership', {}).then(res => {
            res.data.forEach(function (obj, index) {
                obj.checked = false
            })
            _this.setData({
                list: res.data
            })

        })
    },
    // 设置默认地址
    setMoren(e) {
        let _this = this
        let item = e.currentTarget.dataset.item
        let index = e.currentTarget.dataset.index
        item.checked = !item.checked
        let list = this.data.list
        list[index] = item
        if (item.checked) {
            item.is_def = 1
        } else {
            item.is_def = 2
        }
        list.forEach(function (obj, i) {
            if (i != index) {
                obj.is_def = 2
            }
        })
        this.setData({
            //    [`list[${index}]`] :item,
            list
        })
        request._get('/api?method=user.setdefship', { id: item.id }).then(res => {
            getApp().toast(res.msg)

            setTimeout(function () {
                _this.setData({
                    list: []
                })
                _this.getList()
            }, 1000)
        })
    },
    // 删除收货地址
    del(e) {
        let item = e.currentTarget.dataset.item
        let _this = this
        request._get('/api?method=user.removeship', {
            id: item.id
        }).then(res => {
            getApp().toast(res.msg)
            setTimeout(function () {
                _this.setData({
                    list: []
                })
                _this.getList()
            }, 1000)
        })
    },
    // 编辑收货地址
    edit(e) {
        let item = e.currentTarget.dataset.item
        wx.navigateTo({
            url: '/pages/mine/pages/my/add-address/add-address?id=' + item.id,
        })
    },
    /**
     *新增收货地址 
     */
    add() {
        wx.navigateTo({
            url: '../../my/add-address/add-address',
        })
    },
    // 选择收货地址
    selectAddress(e) {
        let { index, item } = e.currentTarget.dataset
        let list = this.data.list
        list[index].checked = !list[index].checked
        list.forEach(function (obj, i) {
            if (i != index) {
                obj.checked = false
            }
        })
        wx.setStorage({
            key: 'addressId',
            data: item.id,
        })
        this.setData({
            list
        })
        setTimeout(function () {
            wx.navigateBack({
                delta: 1, // 回退前 delta(默认为1) 页面
            })
        }, 500)
    },
    // 微信导入
    weixinChat() {
        let that = this
        wx.chooseAddress({
            success: function (resp) {

                //获取到的地址存到data里的areaList中
                that.sendWeixinId(resp).then(tip=>{
                    resp.addressId = tip.data
                    setTimeout(function () {
                        wx.navigateTo({
                            url: '/pages/mine/pages/my/add-address/add-address',
                            success: function (res) {
                                res.eventChannel.emit('acceptDataFromOpenerPage', { data: resp })
                            }
                        })
                    }, 100)
                })
               
            },
            fail: () => {
                // this.openConfirm()   // 如果获取地址权限失败，弹出确认弹窗，让用户选择是否要打开设置，手动去开权限
            }
        })

    },
    // 选择城市id提交
    sendWeixinId(obj) {
        let _this = this
        return new Promise((resolve, reject) => {
            request._post('/api?method=user.getareaid', {
                province_name: obj.provinceName,
                city_name: obj.cityName,
                county_name: obj.countyName,
                postal_code: obj.postalCode
            }).then(res => {
               resolve(res)
            }).catch(res=>{
                reject(res)
            })
        })
    
    },
    // 打开地址获取权限
    openConfirm() {

    }
})